package com.lvmama.rhino.web;


import com.lvmama.rhino.enums.CommonStringType;
import com.lvmama.rhino.pojo.AccuratePushStatVO;
import com.lvmama.rhino.pojo.BasePageInfoVO;
import com.lvmama.rhino.service.AccuratePushService;
import com.lvmama.rhino.utils.FilenameUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description:精准推送统计
 * Date:2017/3/10 17:57
 * Created by wxxuyuan
 */
@Controller
@RequestMapping("accuracy")
public class AccuratePushController {

	private final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	AccuratePushService accuratePushService;

	/**
	 * Description:页面view
	 * Date:2017/3/10 17:59
	 * Created by wxxuyuan
	 */
	@RequestMapping("view")
	public String view(ModelMap map) {
		logger.info("accurate push statistics view############");
		//平台下拉列表
		List<CommonStringType> platForms = new ArrayList<>(Arrays.asList(CommonStringType.getRecommendPlatforms()));
		platForms.remove(3);//推送平台不需要wap
		map.addAttribute("platForms",platForms);
		return "application/accuratePush";
	}

	/**
	 * Description:查询
	 * Date:2017/3/2 16:44
	 * Created by wxxuyuan
	 */
	@RequestMapping("query")
	@ResponseBody
	public BasePageInfoVO<AccuratePushStatVO> listAccuratePush(AccuratePushStatVO vo, HttpSession session){

		List<AccuratePushStatVO> data = accuratePushService.listAccuratePush(vo);
		logger.info("length: " + data.size());

		int rowCount = accuratePushService.countAccuratePush(vo);
		BasePageInfoVO<AccuratePushStatVO> pageInfo = new BasePageInfoVO<>();
		pageInfo.setRowCount(rowCount);
		pageInfo.setData(data);
		session.setAttribute("accuratePushVO",vo);//查询条件放入session供，导出使用
		return pageInfo;
	}

	/**
	 * Description:导出
	 * Date:2017/3/2 16:45
	 * Created by wxxuyuan
	 */
	@RequestMapping("exportData")
	public void exportData(HttpServletRequest request,HttpServletResponse response) throws IOException {
		logger.info("exportData");
		AccuratePushStatVO vo = (AccuratePushStatVO) request.getSession().getAttribute("accuratePushVO");
		vo.setStart(0);
		vo.setPageSize(Integer.MAX_VALUE);//导出所有数据
		List<AccuratePushStatVO> dataList = accuratePushService.listAccuratePush(vo);

		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
		HSSFSheet sheet = hssfWorkbook.createSheet("精准推送转化统计");
		HSSFRow headerRow = sheet.createRow(0);
		headerRow.createCell(0).setCellValue("日期");
		headerRow.createCell(1).setCellValue("推送总量");
		headerRow.createCell(2).setCellValue("点击数");
		headerRow.createCell(3).setCellValue("UV");
		headerRow.createCell(4).setCellValue("总下单数");
		headerRow.createCell(5).setCellValue("下单用户数");
		headerRow.createCell(6).setCellValue("下单总金额(元)");
		headerRow.createCell(7).setCellValue("人均单量");
		headerRow.createCell(8).setCellValue("转化率");
		headerRow.createCell(9).setCellValue("客单价(元)");

        DecimalFormat df = new DecimalFormat("#.00");

		for (AccuratePushStatVO data : dataList) {
			HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
			dataRow.createCell(0).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(data.getDate()));
			dataRow.createCell(1).setCellValue(data.getTotalPushNo());
			dataRow.createCell(2).setCellValue(data.getClickNo());
			dataRow.createCell(3).setCellValue(data.getUv());
			dataRow.createCell(4).setCellValue(data.getOrderTotalNo());
			dataRow.createCell(5).setCellValue(data.getOrderedUserNo());
			dataRow.createCell(6).setCellValue(data.getOrderTotalAmount());
			dataRow.createCell(7).setCellValue(data.getOrderNoPerUser());
			dataRow.createCell(8).setCellValue(df.format(data.getTurnRate()*100)+"%");
			dataRow.createCell(9).setCellValue(data.getAmountPerUser());
		}

		String downloadFilename="精准推送转化统计.xls";
		String contentType=request.getSession().getServletContext().getMimeType(downloadFilename);
		response.setContentType(contentType);
		String agent=request.getHeader("user-agent");
		downloadFilename= FilenameUtils.encodeDownloadFilename(downloadFilename, agent);
		String contentDisposition="attachment;filename="+downloadFilename;
		response.setHeader("Content-Disposition", contentDisposition);
		hssfWorkbook.write(response.getOutputStream());
	}



}
